package com.teleca.jamendo.ui.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.teleca.jamendo.R;
import com.teleca.jamendo.mode.PlaylistEntry;
import com.teleca.jamendo.util.download.DownloadJob;

public class DownloadJobAdapter extends ArrayListAdapter<DownloadJob> {

	public DownloadJobAdapter(Activity context) {
		super(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;

		ViewHolder holder;

		if (row == null) {
			LayoutInflater inflater = mContext.getLayoutInflater();
			row = inflater.inflate(R.layout.download_row, null);

			holder = new ViewHolder();
			holder.songName = (TextView) row.findViewById(R.id.TrackRowName);
			holder.songArtistAlbum = (TextView) row.findViewById(R.id.TrackRowArtistAlbum);
			holder.songProgressText = (TextView) row.findViewById(R.id.TrackRowProgress);
			holder.progressBar = (ProgressBar) row.findViewById(R.id.ProgressBar);
			row.setTag(holder);

			mContext.registerForContextMenu(row);
		} else {
			holder = (ViewHolder) row.getTag();
		}

		PlaylistEntry playlistEntry = mList.get(position).getPlaylistEntry();
		holder.songName.setText(playlistEntry.getTrack().getName());
		holder.songArtistAlbum.setText(playlistEntry.getAlbum().getArtistName() + " - "
				+ playlistEntry.getAlbum().getName());

		if (mList.get(position).getProgress() == 100) {
			holder.progressBar.setVisibility(View.GONE);
			holder.songProgressText.setText("完成");
		} else {
			holder.progressBar.setVisibility(View.VISIBLE);
			holder.progressBar.setMax(100);
			holder.progressBar.setProgress(mList.get(position).getProgress());
			holder.songProgressText.setText(mList.get(position).getProgress() + "%");
		}
		return row;
	}

	static class ViewHolder {
		TextView songName;
		TextView songArtistAlbum;
		TextView songProgressText;
		ProgressBar progressBar;
	}

}
